package com.hjq.service.impl;

import com.hjq.anno.LogOperation;
import com.hjq.exception.ForeignKeyException;
import com.hjq.mapper.DeptMapper;
import com.hjq.pojo.Dept;
import com.hjq.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {
        // int i=1/0;
        return deptMapper.findAll();
    }
    @Override
    public void deleteById(Integer id) {
        Integer count = deptMapper.getCount(id);
        if (count > 0) {
            throw new ForeignKeyException("该部门下有员工，不能删除");
        }
        deptMapper.deleteById(id);
    }

    @Override
    public void insert(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMapper.getById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
